﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Web;
using Kooboo.Connect;
using Kooboo.IoC;


namespace Kooboo.Commerce
{

    [Export(typeof(IActivatorContext))]
    public class ActivatorContext : IActivatorContext
    {
        #region IActivatorContext Members

        public IEnumerable<CreateHandler> GetHandlers()
        {
            return new List<CreateHandler>()
            {
                new CreateHandler(){ 
                    Type = typeof(Kooboo.Connect.User), 
                    Invoker = ()=>
                    {
                        if(HttpContext.Current != null)
                        {
                            var user = HttpContext.Current.Items["current_loged_user"] as Kooboo.Connect.User;

                            if(user == null)
                            {
                                if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
                                {
                                    var userName = HttpContext.Current.User.Identity.Name;
                                    user = UserServices.FindUser(userName);
                                    HttpContext.Current.Items["current_loged_user"] = user;
                                }
                            }

                            return user;
                        }
                        
                        return null;                        
                    }
                }
            };
        }

        #endregion
    }
}
